/**
 * @file rtc.c
 * @brief T1 RTC Driver
 *
 * @author yemt@witsi.cn
 * @date 2011-08-15
 * @version T1.RTC.02
 */
#include <stdint.h>
#include "regs.h"
#include "rtc.h"
#include "dev.h"
#include "init.h"
#include "config.h"
/**
 * @fn get_rtc_seconds
 * @brief Get the RTC seconds
 *
 * @return
 *      uint32_t        The current RTC seconds
 */
uint32_t get_rtc_seconds(void)
{
        return (RTC->RTCDR);
}

/**
 * @fn set_rtc_seconds
 * @brief Set the RTC seconds
 *
 * @parm val            The new RTC seconds to be set
 * @return void
 */
void set_rtc_seconds(uint32_t val)
{
        if (val == RTC->RTCDR) return;
        RTC->RTCLR = val;
        RTC->RTCEN = RTC_EN;
        RTC->LOCK_EN &= ~(RTCEN_LOCK | LOAD_LOCK);
        while (RTC->LOCK_EN != 0x3F);
#ifdef CONFIG_POWER_CTL
        sys_pm_init();
#endif
}

void rtc_enable(void)
{
        RTC->RTCEN = RTC_EN;
        RTC->LOCK_EN &= ~RTCEN_LOCK;
        while (!(RTC->LOCK_EN & RTCEN_LOCK));
}

void rtc_init(void)
{
    RTC->RTCEN = RTC_EN;
    RTC->LOCK_EN &= ~RTCEN_LOCK;
}
late_initcall(rtc_init);


